package puppet.user;

import puppet.student.StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<User> userArrayList = new ArrayList<>();

        while(true){
            System.out.println("欢迎使用学生管理系统");
            System.out.println("请选择：1、登录 2、注册 3、忘记密码 0、退出系统");
            String choice = scanner.next();
            switch (choice){
                case "1" : login(userArrayList);
                    break;
                case "2" : register(userArrayList);
                    break;
                case "3" : forgetPassword(userArrayList);
                    break;
                case "0" :
                    System.out.println("谢谢使用");
                    System.exit(0);
                    break;
                default :
                    System.out.println("输入无效，请重新输入");
                    break;
            }
        }
    }

    //登录
    public static void login(ArrayList<User> userArrayList) {
        System.out.println("login");
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户名userName: ");
        String userName = scanner.next();

        //验证用户是否存在
        int index = isRepeatI(userArrayList,userName);
        //true 重复即存在
        if(index < 0){
            System.out.println("用户不存在，请注册");
            return;
        }

        User user = userArrayList.get(index);

        //验证密码
        int count = 3;
        while(true){
            System.out.println("密码password: ");
            String password = scanner.next();
            if(password.equals(user.getPassword())){
                break;
            }else {
                count--;
                System.out.println("密码输入错误，你还有" + count + "次机会输入");
                if(count == 0){
                    System.out.println("密码输入次数使用完");
                    return;
                }
            }
        }

        //验证验证码
        while(true){
            String strCode = verCode();
            System.out.println(strCode);
            System.out.println("验证码strCode");
            String code = scanner.next();
            if (strCode.equalsIgnoreCase(code)){
                System.out.println("登陆成功");
                break;
            }
            System.out.println("请重新输入");
        }

        StudentSystem system = new StudentSystem();
        system.studentSystemMain();
    }

    //注册
    public static void register(ArrayList<User> userArrayList) {
        System.out.println("register");
        Scanner scanner = new Scanner(System.in);
        String userName = "";

        //输入用户名
        while(true){
            System.out.println("用户名userName: ");
            userName = scanner.next();

            //验证格式是否正确
            boolean flagForm = isNameForm(userName);
            //true 正确
            if(!flagForm){
                System.out.println("格式输入不正确，请重新输入");
                continue;
            }

            //验证用户名的唯一性
            boolean flagOnly = isRepeatB(userArrayList,userName);
            //true 重复
            if(!flagOnly){
                break;
            }

            System.out.println("用户名不唯一，请重新输入");
        }

        //输入密码
        String password = "";
        while(true){
            System.out.println("密码password");
            password = scanner.next();
            System.out.println("再次输入密码");
            if(password.equals(scanner.next())){
                break;
            }
            System.out.println("两次密码不一样，请重新输入");
        }

        //输入身份证
        String userId = "";
        while(true){
            System.out.println("身份证userId");
            userId = scanner.next();
            boolean flagFrom = isIdForm(userId);
            //true 正确
            if(flagFrom){
                break;
            }
            System.out.println("身份证格式不正确，请重新输入");
        }

        //输入电话号码
        String phoneNumber = "";
        while(true){
            System.out.println("电话号码phoneNumber");
            phoneNumber = scanner.next();
            boolean flagFrom = isPhoneForm(userId);
            //true 正确
            if(flagFrom){
                break;
            }
            System.out.println("身份证格式不正确，请重新输入");
        }

        userArrayList.add(new User(userName,password,userId,phoneNumber));

    }

    //忘记密码
    public static void forgetPassword(ArrayList<User> userArrayList) {
        System.out.println("forgetPassword");
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户名userName: ");
        String userName = scanner.next();

        //验证用户是否存在
        int index = isRepeatI(userArrayList,userName);
        //true 重复即存在
        if(index < 0){
            System.out.println("用户不存在，请注册");
            return;
        }

        User user = userArrayList.get(index);

        //验证身份证
        while(true){
            System.out.println("身份证userId: ");
            String userId = scanner.next();
            if(userId.equals(user.getUserId())){
                break;
            }else {
                System.out.println("输入的身份证错误，请重新输入");
            }
        }

        //验证电话号码
        while(true){
            System.out.println("电话号码phoneNumber: ");
            String phoneNumber = scanner.next();
            if(phoneNumber.equals(user.getPhoneNumber())){
                break;
            }else {
                System.out.println("输入的电话号码错误，请重新输入");
            }
        }

        //修改密码
        String newpassword = "";
        while(true){
            System.out.println("密码password");
            newpassword = scanner.next();
            System.out.println("再次输入密码");
            if(newpassword.equals(scanner.next())){
                break;
            }
            System.out.println("两次密码不一样，请重新输入");
        }

        user.setPassword(newpassword);

    }

    //判断用户名是否重复
    //如果重复就返回true
    public static boolean isRepeatB(ArrayList<User> userArrayList,String userName) {
//        for (int i = 0; i < userArrayList.size(); i++) {
//            User user = userArrayList.get(i);
//            if(user.getUserName().equals(userName)){
//                return true;
//            }
//        }
//        return false;
        int index = isRepeatI(userArrayList,userName);
        if(index >= 0) {
            return true;
        }else {
            return false;
        }
    }

    //判断用户名是否重复
    //如果重复就返回索引
    public static int isRepeatI(ArrayList<User> userArrayList,String userName) {
        for (int i = 0; i < userArrayList.size(); i++) {
            User user = userArrayList.get(i);
            if(user.getUserName().equals(userName)){
                return i;
            }
        }
        return -1;
    }

    //判断用户名格式是否符合要求
    //符合要求true
    public static boolean isNameForm (String userName){
        //验证用户名长度
        if(userName.length() <= 3 && userName.length() >= 15){
            return false;
        }
        int count = 0;
        //验证用户名是否符合内容要求
        for (int i = 0; i < userName.length(); i++) {
            char ch = userName.charAt(i);
            if(ch >= '0' && ch <= '9'){
            } else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
                count++;
            } else {
                System.out.println("不符合要求，请重新输入");
                return false;
            }
        }
        //验证用户名是否全部都是数字
        if(count == 0){
            return false;
        }

        return true;
    }

    //验证身份证格式是否正确
    //如果正确返回true
    public static boolean isIdForm (String userId){
        //验证长度
        if(userId.length() != 18){
            return false;
        }

        //验证是否为0开头
        char c = userId.charAt(0);
        if(c == '0'){
            return false;
        }

        //验证是否符合内容要求
        for (int i = 0; i < userId.length() - 1 ; i++) {
            char ch = userId.charAt(i);
            if(!(ch >= '0' && ch <= '9')){
                return false;
            }
        }

        //验证最后一位是否符合要求
        char ch = userId.charAt(userId.length()-1);
        if(!(ch >= '0' && ch <= '9' || ch == 'x' || ch == 'X')){
            return false;
        }

        return true;
    }

    //验证电话号码格式是否正确
    //如果正确返回true
    public static boolean isPhoneForm (String phoneNumber){
        //验证长度
        if(phoneNumber.length() == 11){
            return false;
        }

        //开头不能为0
        if(phoneNumber.charAt(0) == '0'){
            return true;
        }

        //验证是否符合内容要求
        for (int i = 0; i < phoneNumber.length(); i++) {
            char ch = phoneNumber.charAt(i);
            if(!(ch >= '0' && ch <= '9')){
                return false;
            }
        }

        return true;
    }

    //生成验证码
    public static String verCode(){
        String strCode = "";

        //设定数组
        char[] crr = new char[52];
        for (int i = 0; i < 26; i++) {
            crr[i] = (char)(i + 65);
        }
        for (int i = 26; i < crr.length; i++) {
            crr[i] = (char) (97 + i - 26);
        }

        //生成随机验证码
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(crr.length);
            strCode = strCode + crr[index];
        }
        strCode = strCode + random.nextInt(10);

        //数字位置随机
        char[] strCrr = strCode.toCharArray();
        for (int i = 0; i < strCrr.length; i++) {
            int index = random.nextInt(strCrr.length);
            char tmp = strCrr[i];
            strCrr[i] = strCrr[index];
            strCrr[index] = tmp;
        }

        return new String(strCrr);
    }

}
